package com.lalilu.lmedia.indexer

import com.blankj.utilcode.util.FileUtils
import com.lalilu.lmedia.entity.LAlbum
import com.lalilu.lmedia.entity.LArtist
import com.lalilu.lmedia.entity.LDictionary
import com.lalilu.lmedia.entity.LGenre
import com.lalilu.lmedia.entity.LSong

/**
 * 筛选类型
 */
sealed class FilterType {
    data class MediaId(val mediaId: List<String>) : FilterType()
    data class ArtistName(val artistName: List<String>) : FilterType()
    data class AlbumId(val albumId: List<String>) : FilterType()
    data class Path(val path: List<String>) : FilterType()
    data class Duration(val second: Int) : FilterType()

    /**
     * 筛除未知艺术家的歌曲
     */
    object UnknownArtist : FilterType()

    /**
     * 默认不处理
     */
    object None : FilterType()
}

@JvmName("filterBySong")
fun List<LSong>.filterBy(list: List<FilterType>): List<LSong> = apply {
    list.forEach { type ->
        when (type) {
            is FilterType.AlbumId -> onEach {
                if (it._albumId in type.albumId) {
                    it.blocked = true
                }
            }

            is FilterType.ArtistName -> onEach {
                if (it._artist in type.artistName) {
                    it.blocked = true
                }
            }

            is FilterType.MediaId -> onEach {
                if (it.id in type.mediaId) {
                    it.blocked = true
                }
            }

            is FilterType.Path -> onEach {
                val path = FileUtils.getDirName(it.pathStr)
                    ?.takeIf(String::isNotEmpty)
                    ?: "Unknown dir"
                if (path in type.path) {
                    it.blocked = true
                }
            }

            FilterType.UnknownArtist -> onEach {
                if (it._artist.contains("<unknown>")) {
                    it.blocked = true
                }
            }

            else -> {}
        }
    }
}

@JvmName("filterByArtist")
fun List<LArtist>.filterBy(list: List<FilterType>): List<LArtist> = apply {
    list.forEach { type ->
        when (type) {
            is FilterType.ArtistName -> onEach {
                if (it.name in type.artistName) {
                    it.blocked = true
                }
            }

            else -> {}
        }
    }
}

@JvmName("filterByAlbum")
fun List<LAlbum>.filterBy(list: List<FilterType>): List<LAlbum> = apply {
    list.forEach { type ->
        when (type) {
            is FilterType.AlbumId -> onEach {
                if (it.id in type.albumId) {
                    it.blocked = true
                }
            }

            else -> {}
        }
    }
}

@JvmName("filterByDictionary")
fun List<LDictionary>.filterBy(list: List<FilterType>): List<LDictionary> = apply {
    list.forEach { type ->
        when (type) {
            is FilterType.Path -> onEach {
                if (it.path in type.path) {
                    it.blocked = true
                }
            }

            else -> {}
        }
    }
}

@JvmName("filterByGenre")
fun List<LGenre>.filterBy(list: List<FilterType>): List<LGenre> = apply {
}